<template>

	<!-- 编辑制程报检单 -->
	<view>

		<u-toast ref="uToast"></u-toast><!-- 弹窗 -->

		<view class="form-container">
			<!-- :rules表单校验规则，通过rules属性来指定表单项的校验规则 ref:给表单设置一个引用名称 errorType设置表单错误提示方式
			label-position表单项的标签位置为左对齐 label-align设置标签文字对齐方式为左对齐-->
			<u-form :model="dataForm" :rules="rules" ref="dataForm" :errorType="['toast']" label-position="left"
				label-width="150" label-align="left" class="shzy-form">

				<!-- 报检类型 -->
				<u-form-item label="报检类型" prop="type" label-width="180" required class="form-item">
					<shzy-select v-model="dataForm.type" placeholder="请选择" :options="typeList"
						:props="{label:'fullName',value:'enCode'}"></shzy-select>
				</u-form-item>

				<!-- 工厂信息 -->
				<u-form-item label="工厂信息" prop="workshopId" label-width="180" required class="form-item">
					<shzy-tree-select v-model="factory" :options="workshopIdList"
						@changeFun="FactoryConfirm">
					</shzy-tree-select>
				</u-form-item>

				<!-- 物料 -->
				<u-form-item label="物料" prop="materialCode" label-width="180" required class="form-item"
					onscroll="handleScroll">
					<shzy-select v-model="dataForm.materialCode" placeholder="请选择" :options="materialCodeList" lazy
						:props="{label:'name',value:'code'}" isConcat></shzy-select>
				</u-form-item>

				<!-- 工序 -->
				<u-form-item label="工序" prop="processIdList" label-width="180" class="form-item">
					<shzy-select v-model="dataForm.processIdList" placeholder="请选择" :options="processIdLists"
						:props="{label:'fullName',value:'enCode'}"></shzy-select>
				</u-form-item>

				<!-- 检验方案 -->
				<u-form-item label="检验方案" prop="inspectionCase" label-width="180" required class="form-item">
					<shzy-select v-model="dataForm.inspectionCase" placeholder="请选择" :options="inspectionCaseList"
						:props="{label:'name',value:'id'}"></shzy-select>
				</u-form-item>

				<!-- 生产订单号 -->
				<u-form-item label="生产订单号" prop="orderNo" label-width="180" required class="form-item">
					<u-input v-model="dataForm.orderNo" placeholder="请输入"></u-input>
				</u-form-item>

				<!-- 生产数量 -->
				<u-form-item label="生产数量" prop="productionQty" label-width="180" required class="form-item">
					<u-input v-model="dataForm.productionQty" placeholder="请输入" type="number"></u-input>
				</u-form-item>

				<!-- 检验员 -->
				<u-form-item label="检验员" prop="employeeName" label-width="180" class="form-item">
					<shzy-org-select v-model="dataForm.employeeCode" 
						@changeFun="employeeChange" :default-expand-all="false"></shzy-org-select>
				</u-form-item>

				<!-- 报检日期 -->
				<u-form-item label="报检日期" prop="date" label-width="180" required class="form-item">

					<shzy-date-time type="datetime" v-model="dataForm.date"
						:disabled="dataForm.dayFlag == '1' || dataForm.hourFlag == '1'"></shzy-date-time>
				</u-form-item>

				<!-- 按天生成 -->
				<u-form-item label="按天生成" prop="dayFlag" label-width="180" class="form-item" required>
					<view class="u-flex u-form-item-switch">
						<shzy-switch v-model="dataForm.dayFlag" :disabled="dataForm.hourFlag == '1'"></shzy-switch>
					</view>
				</u-form-item>

				<!-- 天 -->
				<u-form-item label="天" prop="dayValue" label-width="180" required class="form-item">
					<u-input v-model="dataForm.dayValue" placeholder="请输入"
						:disabled="dataForm.dayFlag == '0'"></u-input>
				</u-form-item>

				<!-- 按小时生成 -->
				<u-form-item label="按小时生成" prop="hourFlag" label-width="180" class="form-item" required>
					<view class="u-flex u-form-item-switch">
						<shzy-switch v-model="dataForm.hourFlag" :disabled="dataForm.dayFlag == '1'"></shzy-switch>
					</view>
				</u-form-item>

				<!-- 小时 -->
				<u-form-item label="小时" prop="hourValue" label-width="180" required class="form-item">
					<u-input v-model="dataForm.hourValue" placeholder="请输入"
						:disabled="dataForm.hourFlag == '0'"></u-input>
				</u-form-item>

				<!-- 自动关闭 -->
				<u-form-item label="自动关闭" prop="closeFlag" label-width="180" required class="form-item">
					<view class="u-flex u-form-item-switch">
						<shzy-switch v-model="dataForm.closeFlag"></shzy-switch>
					</view>
				</u-form-item>

				<!-- 备注 -->
				<u-form-item label="备注" prop="remark" label-width="180" class="form-item">
					<u-input v-model="dataForm.remark" placeholder="请输入"></u-input>
				</u-form-item>

			</u-form>

		</view>
		<!-- 按钮 -->
		<view class="buttom-actions-addsample">
			<u-button class="buttom-btn" type="primary" @click="submit()">确认</u-button>
		</view>

	</view>

</template>

<script>
	import {
		GetQmsBaseFactorystructure, // 工厂信息
		GetMaterialGroup, // 物料
		Getcheckcard, //检验类型
		GetModeling, // 工序
		GetEditInfo, // 获取报检单
		InsertIPQCInspection ,// 新增制程报检单
		InsertIPQCInspectionPut
	} from '@/api/q1/business/inspection/ipqcinspection.js'
	import {
		getDictionaryDataSelector, // 字典项【报检类型】
		// getUserSelector
	} from '@/api/common.js'
	export default {
		data() {
			
			const qtyValidator = (rule, value, callback) => {
				if (value.length == 0) {
					callback(new Error("请输入生产数量"));
				} else {
					var _reg = /^\d+$/
					if (!_reg.test(value)) {
						callback(new Error("生产数量必须为整数"));
					}
				}
			};
			
			return {
				dataForm: {

					closeFlag: 0, // 自动关闭 
					code: '',
					date: 0, // 报检日期 
					dayFlag: 0, // 按天生成
					dayValue: 0, // 天
					employeeCode: '',
					employeeName: '', // 检验员 
					factoryId: '',
					hourFlag: 0, // 按小时生成 
					hourValue: 0, // 小时 
					id: '', // 表单id
					inspectionCase: '', // 检验方案 
					materialCode: '', // 物料 
					orderNo: '', // 生产订单号
					overdue: 0,
					processData:[],// 工序集合
					processIdList: '', // 工序
					processResult: [],
					productionQty: 0, // 生产数量 
					remark: '', // 备注 
					stationId: '',
					status: 0,
					teamId: '',
					type: '', // 报检类型 
					workshopId: '', // 工厂信息
					workshopSectionId: "",
					
				}, // 表单
				rules: {
					type: [{
						required: true,
						message: '请选择报检类型',
						trigger: 'blur',
					}],
					workshopId: [{
						required: true,
						message: '请选择工厂信息',
						trigger: 'blur',
					}],
					materialCode: [{
						required: true,
						message: '请选择物料',
						trigger: 'blur',
					}],
					inspectionCase: [{
						required: true,
						message: '请选择检验方案',
						trigger: 'blur',
					}],
					orderNo: [{
						required: true,
						message: '请输入生产订单号',
						trigger: 'blur',
					}],
					productionQty: [{
						required: true,
						validator: qtyValidator,
						trigger: 'blur',
					}]
					
					
				}, // 校验规则
				// 下拉框数组
				typeList: [], // 报检类型
				workshopIdList: [], // 工厂信息 	
				materialCodeList: [], // 物料 
				processIdLists: [], // 工序 
				inspectionCaseList: [], // 检验方案 
				pageType: '',
				factory:''
			}
		},
		onReady() {
			this.$refs.dataForm.setRules(this.rules); // 设置校验规则
		},
		onLoad(e) {
			this.pageType = e.pageType;
			// 加载编辑信息
			if (this.pageType == 'edit') {
				this.dataForm.id = e.id;
				this.getEditInfo();
			}
			this.getSelect(); // 加载下拉框
		},
		mounted() {
			uni.setNavigationBarTitle({
				title: this.pageType == 'edit' ? '编辑' : '新增'
			})
		},
		methods: {
			// 加载编辑信息
			getEditInfo() {

				GetEditInfo(this.dataForm.id).then(res => {
					
					this.dataForm = res.data
					
					this.dataForm.processResult = [];
					this.dataForm.dayFlag = this.dataForm.dayFlag == 'Y' ? 1 : 0;
					this.dataForm.hourFlag = this.dataForm.hourFlag == 'Y' ? 1 : 0;
					this.dataForm.closeFlag = this.dataForm.closeFlag == 'Y' ? 1: 0;
					
					console.log(this.dataForm)
					// 显示工厂信息
					this.factory = this.dataForm.workshopId != '' ? this.dataForm.workshopId : this.factory;
					this.factory = this.dataForm.teamId != '' ? this.dataForm.teamId : this.factory;
					this.factory = this.dataForm.workshopSectionId != '' ? this.dataForm.workshopSectionId : this.factory;
					this.factory = this.dataForm.stationId != '' ? this.dataForm.stationId : this.factory;
				})

			},

			// 加载下拉框
			getSelect() {

				// 报检类型
				getDictionaryDataSelector('465670858640243269').then(res => {
					this.typeList = res.data.list;
				}).catch((err) => {
					this.$refs.uToast.show({
						title: '异常:' + err,
						type: 'error'
					})
				});

				// 工厂信息
				GetQmsBaseFactorystructure().then(res => {
					this.workshopIdList = res.data.list;
				}).catch((err) => {
					this.$refs.uToast.show({
						title: '异常:' + err,
						type: 'error'
					})
				});

				// 物料
				GetMaterialGroup().then(res => {
					this.materialCodeList = res.data.list;
				}).catch((err) => {
					this.$refs.uToast.show({
						title: '异常:' + err,
						type: 'error'
					})
				});

				// 工序
				GetModeling().then(res => {
					this.processIdLists = res.data.list;
				}).catch((err) => {
					this.$refs.uToast.show({
						title: '异常:' + err,
						type: 'error'
					})
				});

				//检验方案
				Getcheckcard().then(res => {
					this.inspectionCaseList = res.data.list;
				}).catch((err) => {
					this.$refs.uToast.show({
						title: '异常:' + err,
						type: 'error'
					})
				});

			},
			// 确认
			submit() {

				this.$refs.dataForm.validate(valid => {
					
					if (valid && this.check() == true) {
						console.log('true')
						if (this.pageType == 'insert') {
							// 新增
							this.dataForm.factoryId = this.dataForm.workshopId
							const data = [];
							data.push(this.dataForm);
							InsertIPQCInspection(data).then(res => {
								if (res.code == 200) {
									this.$refs.uToast.show({
										title: "提交成功",
										type: 'success',
										duration: 1000,
										callback() {
											uni.navigateBack(); // 返回上一页
										}
									});
								}
							})
						}else{
							
							InsertIPQCInspectionPut(this.dataForm.id,this.dataForm).then(res => {
								if (res.code == 200) {
									this.$refs.uToast.show({
										title: "提交成功",
										type: 'success',
										duration: 1000,
										callback() {
											uni.navigateBack(); // 返回上一页
										}
									});
								}
							})
						}
						
						this.dataForm.dayFlag = 0;
						this.dataForm.hourFlag = 0;
						this.dataForm.closeFlag = 0;
					} else {
						console.log('false')
					}
				});
			},

			// 检查
			check() {
				try {
					var errMsg = '';
					errMsg = this.dataForm.productionQty === '' ? '请输入生产数量' : errMsg;
					errMsg = errMsg == '' && this.dataForm.date == '' ? '请选择报检日期' : errMsg;
					if (errMsg != '') {

						this.$refs.uToast.show({
							title: errMsg,
							type: 'default'
						})
						return false;
					}

					// 转化
					this.dataForm.dayFlag = this.dataForm.dayFlag == 0 ? 'N' : 'Y'
					this.dataForm.hourFlag = this.dataForm.hourFlag == 0 ? 'N' : 'Y'
					this.dataForm.closeFlag = this.dataForm.closeFlag == 0 ? 'N' : 'Y'

					return true;
				} catch (e) {
					console.log(e)
				}

			},
			// 获取检验员信息
			employeeChange(e, data) {
				console.log(data)
				this.dataForm.employeeCode = data.id;
				this.dataForm.employeeName = data.fullName;
			},

		
			// 获取工厂信息
			FactoryConfirm(e, data) {
				
				// 车间
				var arry = this.workshopIdList;
				
				for (let i = 0; i < arry.length; i++) {
					var id = arry[i].id;
					if (id == e) {
						this.dataForm.workshopId = e;
						this.dataForm.teamId = '';
						this.dataForm.workshopSectionId = '';
						this.dataForm.stationId = '';
						return;
					} else if (arry[i].hasChildren) {
						this.dataForm.workshopId = id;
						var children = arry[i].children;
						for (let j = 0; j < children.length; j++) {
							id = children[j].id;
							if (id == e) {
								this.dataForm.teamId = e;
								this.dataForm.workshopSectionId = '';
								this.dataForm.stationId = '';
								return;
							} else if (children[j].hasChildren) {
								this.dataForm.teamId = id;
								var grandchildren = children[j].children;
								for (let k = 0; k < grandchildren.length; k++) {
									id = grandchildren[k].id;
									if (id == e) {
										this.dataForm.workshopSectionId = e;
										this.dataForm.stationId = '';
										return;
									} else if (grandchildren[k].hasChildren) {
										this.dataForm.workshopSectionId = id;
										var greatGrandchildren = grandchildren[k].children;
										for (let l = 0; l < greatGrandchildren.length; l++) {
											id = greatGrandchildren[l].id;
											if (id == e) {
												this.dataForm.stationId = e;
												return;
											}
										}
									}
								}
							}
						}
					}
				}

			}
		}
	}
</script>

<style lang="scss">
	.shzy-form {
		margin: 0 40rpx 0 40rpx;
	}

	.form-container {
		padding-bottom: 100rpx;
	}

	.buttom-actions-addsample {
		background-color: #fff;
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 44px;
		box-shadow: 0 -1px 4px #e1e5ec;
		z-index: 20;

		.buttom-btn {
			width: 100%;
			/* #ifndef MP */
			height: 88rpx !important;
			line-height: 88rpx !important;
			border-radius: 0 !important;

			&::after {
				border: none !important;
			}

			/* #endif */
			/* #ifdef MP */
			.u-btn {
				width: 100%;
				height: 88rpx !important;
				line-height: 88rpx !important;
				border-radius: 0 !important;

				&::after {
					border: none !important;
				}
			}

			/* #endif */
		}
	}
</style>